import type { VbenFormSchema } from '@vben/common-ui';
import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { LimbDeviceFirmwareApi } from '#/api';

import { getDeviceModelOptions } from '#/api';
import { $t } from '#/locales';

export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('limb.device-firmware.name'),
    },
    {
      component: 'Input',
      fieldName: 'version',
      label: $t('limb.device-firmware.version'),
    },
    {
      component: 'ApiSelect',
      componentProps: {
        mode: 'multiple',
        style: { width: '100%' },
        api: getDeviceModelOptions,
        showSearch: true,
        allowClear: true,
        filterOption: (input: string, option: any) => {
          return option?.label?.toLowerCase().includes(input.toLowerCase());
        },
      },
      fieldName: 'models',
      label: $t('limb.device-model.name'),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      defaultValue: 1,
      fieldName: 'status',
      label: $t('limb.device-firmware.status'),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          {
            label: $t('limb.device-firmware.releaseState-option.not-release'),
            value: 0,
          },
          {
            label: $t('limb.device-firmware.releaseState-option.debug'),
            value: 1,
          },
          {
            label: $t('limb.device-firmware.releaseState-option.release'),
            value: 2,
          },
        ],
        optionType: 'button',
      },
      defaultValue: 0,
      fieldName: 'releaseState',
      label: $t('limb.device-firmware.releaseState-option.not-release'),
    },
    {
      component: 'Input',
      fieldName: 'fileData',
      label: $t('limb.device-firmware.file'),
    },
    {
      component: 'Textarea',
      fieldName: 'description',
      label: $t('limb.device-firmware.description'),
    },
  ];
}

export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'name',
      label: $t('limb.device-firmware.name'),
    },
    {
      component: 'Input',
      fieldName: 'version',
      label: $t('limb.device-firmware.version'),
    },
    {
      component: 'Select',
      componentProps: {
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      fieldName: 'status',
      label: $t('limb.device-firmware.status'),
    },
    {
      component: 'Select',
      componentProps: {
        options: [
          {
            label: $t('limb.device-firmware.releaseState-option.not-release'),
            value: 0,
          },
          {
            label: $t('limb.device-firmware.releaseState-option.debug'),
            value: 1,
          },
          {
            label: $t('limb.device-firmware.releaseState-option.release'),
            value: 2,
          },
        ],
        optionType: 'button',
      },
      fieldName: 'releaseState',
      label: $t('limb.device-firmware.releaseState'),
    },
    {
      component: 'Input',
      fieldName: 'description',
      label: $t('limb.device-firmware.description'),
    },
  ];
}

export function useColumns<T = LimbDeviceFirmwareApi.LimbDeviceFirmware>(
  onActionClick: OnActionClickFn<T>,
  onStatusChange?: (newStatus: any, row: T) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'name',
      title: $t('limb.device-firmware.name'),
      width: 100,
    },
    {
      field: 'version',
      title: $t('limb.device-firmware.version'),
      width: 100,
    },
    {
      field: 'description',
      title: $t('limb.device-firmware.description'),
      width: 100,
    },
    {
      cellRender: {
        attrs: { beforeChange: onStatusChange },
        name: onStatusChange ? 'CellSwitch' : 'CellTag',
      },
      field: 'status',
      title: $t('limb.device-firmware.status'),
      width: 100,
    },
    {
      field: 'releaseState',
      title: $t('limb.device-firmware.releaseState'),
      width: 100,
    },
    {
      field: 'remark',
      minWidth: 100,
      title: $t('limb.device-firmware.description'),
    },
    {
      field: 'createTime',
      title: $t('limb.device-firmware.createAt'),
      width: 200,
    },
    {
      align: 'center',
      cellRender: {
        attrs: {
          nameField: 'name',
          nameTitle: $t('limb.device-firmware.name'),
          onClick: onActionClick,
        },
        name: 'CellOperation',
      },
      field: 'operation',
      fixed: 'right',
      title: $t('system.user.operation'),
      width: 130,
    },
  ];
}
